package com.youlai.boot.factory.controller;

import com.youlai.boot.factory.mapper.ProcessMapper;
import com.youlai.boot.factory.model.entity.Process;
import com.youlai.boot.factory.model.vo.ImportStyleProcessVo;
import com.youlai.boot.factory.service.StyleProcessService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.factory.model.form.StyleProcessForm;
import com.youlai.boot.factory.model.query.StyleProcessQuery;
import com.youlai.boot.factory.model.vo.StyleProcessVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 款号工序关联前端控制层
 *
 * @author youlaitech
 * @since 2025-10-06 11:07
 */
@Tag(name = "款号工序关联接口")
@RestController
@RequestMapping("/api/v1/style-process")
@RequiredArgsConstructor
public class StyleProcessController {

    private final StyleProcessService styleProcessService;

    @Operation(summary = "款号工序关联分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('factory:style-process:query')")
    public PageResult<StyleProcessVO> getStyleProcessPage(StyleProcessQuery queryParams) {
        IPage<StyleProcessVO> result = styleProcessService.getStyleProcessPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "款号工序关联分页列表-关联工序表")
    @GetMapping("/joinPage")
    @PreAuthorize("@ss.hasPerm('factory:style-process:query')")
    public PageResult<StyleProcessVO> getStyleProcessJoinPage(StyleProcessQuery queryParams) {
        IPage<StyleProcessVO> result = styleProcessService.getStyleProcessJoinPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增款号工序关联")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('factory:style-process:add')")
    public Result<Void> saveStyleProcess(@RequestBody @Valid StyleProcessForm formData) {
        boolean result = styleProcessService.saveStyleProcess(formData);
        return Result.judge(result);
    }

    @Autowired
    private ProcessMapper processMapper;

    @Operation(summary = "获取款号工序关联表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('factory:style-process:edit')")
    public Result<StyleProcessForm> getStyleProcessForm(
            @Parameter(description = "款号工序关联ID") @PathVariable Long id
    ) {
        StyleProcessForm formData = styleProcessService.getStyleProcessFormData(id);
        //查询工序表
        Process process = processMapper.selectById(formData.getProcessId());
        formData.setProcessName(process.getProcessName());
        return Result.success(formData);
    }

    @Operation(summary = "修改款号工序关联")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('factory:style-process:edit')")
    public Result<Void> updateStyleProcess(
            @Parameter(description = "款号工序关联ID") @PathVariable Long id,
            @RequestBody @Validated StyleProcessForm formData
    ) {
        boolean result = styleProcessService.updateStyleProcess(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除款号工序关联")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('factory:style-process:delete')")
    public Result<Void> deleteStyleProcesss(
            @Parameter(description = "款号工序关联ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = styleProcessService.deleteStyleProcesss(ids);
        return Result.judge(result);
    }


    @Operation(summary = "根据工序模板id批量导入工序")
    @PostMapping("/import")
    @PreAuthorize("@ss.hasPerm('factory:style-process:add')")
    public Result<Void> importStyleProcess(@Parameter(description = "工序模板ID")  @RequestBody @Validated ImportStyleProcessVo importStyleProcessVo) {
        int result = styleProcessService.importStyleProcess(importStyleProcessVo);
        return Result.judge(result > 0);
    }
}
